
using System;
using System.Reflection;
using System.Collections;
using UniversalStorageServices;
using UniversalStorageServices.PersistenceManager;

		

namespace Example
{
		
	public interface ICustomer
	{
		string Id { get; set; }
		System.String Name { get; set; }
		System.String Firstname { get; set; }
		System.String Address { get; set; }
		System.String City { get; set; }
		
		IOrder[]  Orders { get; }

	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Customer'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='CustomerCollection'/>
	[Serializable()]
	public class  CustomerCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CustomerCollection'/>.
		/// </para>
		/// </summary>
		public CustomerCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CustomerCollection'/> based on another <see cref='CustomerCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='CustomerCollection'/> from which the contents are copied
		/// </param>
		public CustomerCollection( CustomerCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CustomerCollection'/> containing any array of <see cref='Customer'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Customer'/> objects with which to intialize the collection
		/// </param>
		public CustomerCollection( Customer[] val)
		{
			this.AddRange(val);
		}
		
		public CustomerCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Customer'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Customer this[int index] 
		{
			get 
			{
				return ((Customer)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Customer'/> with the specified value to the 
		/// <see cref='CustomerCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Customer'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='CustomerCollection.AddRange'/>
		public int Add( Customer val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='CustomerCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='CustomerCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='CustomerCollection.Add'/>
		public void AddRange( CustomerCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Customer[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='CustomerCollection'/> contains the specified <see cref='Customer'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Customer'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Customer'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='CustomerCollection.IndexOf'/>
		public bool Contains( Customer val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='CustomerCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='CustomerCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='CustomerCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Customer[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Customer'/> in 
		/// the <see cref='CustomerCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Customer'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Customer'/> of <paramref name='val'/> in the 
		/// <see cref='CustomerCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='CustomerCollection.Contains'/>
		public int IndexOf( Customer val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Customer'/> into the <see cref='CustomerCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Customer'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='CustomerCollection.Add'/>
		public void Insert(int index,  Customer val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='CustomerCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  CustomerEnumerator GetEnumerator()
		{
			return new  CustomerEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Customer'/> from the 
		/// <see cref='CustomerCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Customer'/> to remove from the <see cref='CustomerCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Customer val)
		{
			List.Remove(val);
		}
		
		public class  CustomerEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  CustomerEnumerator( CustomerCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Customer Current 
			{
				get 
				{
					return (( Customer)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Customer :  IPersistable, ICustomer 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Customer()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
			if(_Orders != null)
				_PersistenceManager.TrackObject(_Orders);
		
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Name
		{
			get 
			{ 
				if(_Entity.GetValue("Name") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Name"]; 
			}
		
			set 
			{ 
				this._Entity["Name"] = value; 
			}
		}
		
		public System.String Firstname
		{
			get 
			{ 
				if(_Entity.GetValue("Firstname") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Firstname"]; 
			}
		
			set 
			{ 
				this._Entity["Firstname"] = value; 
			}
		}
		
		public System.String Address
		{
			get 
			{ 
				if(_Entity.GetValue("Address") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Address"]; 
			}
		
			set 
			{ 
				this._Entity["Address"] = value; 
			}
		}
		
		public System.String City
		{
			get 
			{ 
				if(_Entity.GetValue("City") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["City"]; 
			}
		
			set 
			{ 
				this._Entity["City"] = value; 
			}
		}
		
		protected OrderCollection _Orders;
	
		public OrderCollection  Orders
		{
			get
			{
				if (_Orders == null)
				{
					if(_PersistenceManager == null) // Standalone collection
						_Orders = new OrderCollection(_Entity, "Orders", new IPersistableCollection());
					else
					{
						IPersistableCollection refs = _PersistenceManager.LoadReference(_Entity, "Orders") ;
						_Orders = new OrderCollection(_Entity, "Orders", refs) ;
					}
				}
				return _Orders;
			}	

			set
			{
				_Orders = value;
			}
		}	

		IOrder[] ICustomer.Orders
		{	                
			get 
			{ 
				IPersistable[] result = new IPersistable[Orders.Count];
				Orders.CopyTo(result, 0);
				return (IOrder[])result; 
			}
		}
	
		public Customer ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Customer (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
		}
	}
	
	public interface IOrder
	{
		string Id { get; set; }
		System.String ProductName { get; set; }
		System.Double Amount { get; set; }
		
	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Order'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='OrderCollection'/>
	[Serializable()]
	public class  OrderCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='OrderCollection'/>.
		/// </para>
		/// </summary>
		public OrderCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='OrderCollection'/> based on another <see cref='OrderCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='OrderCollection'/> from which the contents are copied
		/// </param>
		public OrderCollection( OrderCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='OrderCollection'/> containing any array of <see cref='Order'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Order'/> objects with which to intialize the collection
		/// </param>
		public OrderCollection( Order[] val)
		{
			this.AddRange(val);
		}
		
		public OrderCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Order'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Order this[int index] 
		{
			get 
			{
				return ((Order)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Order'/> with the specified value to the 
		/// <see cref='OrderCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Order'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='OrderCollection.AddRange'/>
		public int Add( Order val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='OrderCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='OrderCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='OrderCollection.Add'/>
		public void AddRange( OrderCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Order[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='OrderCollection'/> contains the specified <see cref='Order'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Order'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Order'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='OrderCollection.IndexOf'/>
		public bool Contains( Order val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='OrderCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='OrderCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='OrderCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Order[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Order'/> in 
		/// the <see cref='OrderCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Order'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Order'/> of <paramref name='val'/> in the 
		/// <see cref='OrderCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='OrderCollection.Contains'/>
		public int IndexOf( Order val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Order'/> into the <see cref='OrderCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Order'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='OrderCollection.Add'/>
		public void Insert(int index,  Order val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='OrderCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  OrderEnumerator GetEnumerator()
		{
			return new  OrderEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Order'/> from the 
		/// <see cref='OrderCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Order'/> to remove from the <see cref='OrderCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Order val)
		{
			List.Remove(val);
		}
		
		public class  OrderEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  OrderEnumerator( OrderCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Order Current 
			{
				get 
				{
					return (( Order)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Order :  IPersistable, IOrder 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Order()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String ProductName
		{
			get 
			{ 
				if(_Entity.GetValue("ProductName") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["ProductName"]; 
			}
		
			set 
			{ 
				this._Entity["ProductName"] = value; 
			}
		}
		
		public System.Double Amount
		{
			get 
			{ 
				if(_Entity.GetValue("Amount") == null)
					return (new System.Double[1])[0];
				else
					return (System.Double)this._Entity["Amount"]; 
			}
		
			set 
			{ 
				this._Entity["Amount"] = value; 
			}
		}
		
		public Order ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Order (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
			public Customer GetCustomer()
			{
				IPersistableCollection items = _Owner.PersistenceManager.Load(typeof(Customer), String.Format("[count(Orders[Id = '{0}']) > 0]", _Owner.Id));
			
				if (items.Count > 0)
					return (Customer)items[0];
				else
					return null;
			}
	
		}
	}

}
